package gui.wizard;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.*;
import data.logic.*;
import maiaclient.Maia;

public class CheckDataPanelDescriptor extends WizardPanelDescriptor {

    public static final String IDENTIFIER = "CHECKGAMEDATA";
    private XmlServer selectedServer;
    private UserDataPanel pnlUsrData;
    private MaiaDataPanel pnlMaiaData;
    CheckDataPanel panel3;

    public CheckDataPanelDescriptor(XmlServer selectedServer, UserDataPanel pnlUsrData, MaiaDataPanel pnlMaiaData) {

        panel3 = new CheckDataPanel();
        setPanelDescriptorIdentifier(IDENTIFIER);
        setPanelComponent(panel3);
        this.selectedServer = selectedServer;
        this.pnlUsrData = pnlUsrData;
        this.pnlMaiaData = pnlMaiaData;



    }

    @Override
    public Object getNextPanelDescriptor() {
        return FINISH;
    }

    @Override
    public Object getBackPanelDescriptor() {
        return MaiaDataDescriptor.IDENTIFIER;
    }

    @Override
    public void aboutToDisplayPanel() {

        panel3.setProgressValue(0);
        panel3.setProgressText("Connecting to server...");

        getWizard().setNextFinishButtonEnabled(false);
        getWizard().setBackButtonEnabled(false);

    }

    @Override
    public void displayingPanel() {

        Thread t = new Thread() {

            @Override
            public void run() {

                panel3.getLblVillageNameTest().setText("");
                panel3.getLblusernameTest().setText("");
                try {
                    Thread.sleep(300);
                    // Try to connect to the server
                    Maia maia = new Maia(selectedServer.getIpadres(), selectedServer.getPoort());
                    // Open Connection
                    maia.getMaiaClient().connect();
                    
                    panel3.setProgressText("Making preparations.");
                    panel3.setProgressValue(25);
                    Thread.sleep(300);

                    panel3.setProgressText("Checking Username.");
                    boolean bUserNameCheck = maia.checkAvailableUsername(pnlUsrData.getTxtUsername().getText().toLowerCase());
                    panel3.setUsertest(bUserNameCheck);
                    panel3.setProgressValue(50);
                    Thread.sleep(300);


                    panel3.setProgressText("Checking Village Name.");
                    boolean bVillageNameCheck = maia.checkAvailableVillageName(pnlMaiaData.getTxtVillage().getText());
                    panel3.setVillageTest(bVillageNameCheck);
                    panel3.setProgressValue(75);
                    Thread.sleep(300);
                    panel3.setProgressText("End of verification");
                    panel3.setProgressValue(100);

                    maia.getMaiaClient().closeConnection();

                    if (bVillageNameCheck && bUserNameCheck) {
                        getWizard().setNextFinishButtonEnabled(true);
                    }


                } catch (IOException ex) {

                    JOptionPane.showMessageDialog(getPanelComponent(), "The World of Maia cannot be reached at the moment." +
                            "Please try again later.", "No connection ", JOptionPane.WARNING_MESSAGE);

                    panel3.setProgressValue(0);
                    panel3.setProgressText("An Error Has Occurred");

                } catch (InterruptedException e) {

                } finally {
                    getWizard().setBackButtonEnabled(true);
                }
            }
        };

        t.start();
    }

    @Override
    public void aboutToHidePanel() {
    //  Can do something here, but we've chosen not not.
    }
}
